package com.twq.oo.traits

import com.twq.oo.classes.Rational

/**
 * Created by tangweiqun on 2017/11/28.
 */
object ThinInterfaceRicher {

  //瘦接口(thin interface)是指方法很少的接口，这种接口呢，对于开发者来说是很好实现
  //富接口(rich interface)是指方法很多的接口，这种接口呢，对于调用者客户端来说比较方便，我可以选择任何我想要的方法
  //java中一般都是瘦接口，比如java.lang.CharSequence
  //scala中可以通过trait来使得瘦接口变成富接口，方法是：在trait中定义少数的抽象接口，然后定义大量的具体接口
  //看两个例子
  new Rational(1, 2).<(new Rational(2, 3))
}


//1、第一个例子
class Point(val x: Int, val y: Int)

class Rectangle(val topLeft: Point, val bottomRight: Point) {
  def left = topLeft.x

  def right = bottomRight.x

  def width = right - left

  // and many more geometric methods...
}

abstract class Component {
  def topLeft: Point

  def bottomRight: Point

  def left = topLeft.x

  def right = bottomRight.x

  def width = right - left

  // and many more geometric methods...
}


trait Rectangular {
  def topLeft: Point

  def bottomRight: Point

  def left = topLeft.x

  def right = bottomRight.x

  def width = right - left

  // and many more geometric methods...
}

//trait将瘦接口变成富接口
class RectangleNew(val topLeft: Point, val bottomRight: Point) extends Rectangular {
  // other methods...
}

//trait将瘦接口变成富接口
abstract class ComponentNew extends Rectangular {
  // other methods...
}


